package com.hyfd.deal.Flow;

import java.net.SocketTimeoutException;
import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.hyfd.common.GenerateData;
import com.hyfd.common.utils.Base64Utils;
import com.hyfd.common.utils.JsonHelper;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.MapUtils;
import com.hyfd.common.utils.PostUtil;
import com.hyfd.common.utils.PublicRSA;
import com.hyfd.common.utils.Sign;
import com.hyfd.common.utils.ToolDateTime;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.deal.BaseDeal;

/**
 * 菠萝蜜单号码流量充值接口
 * 
 * @author cxj
 *
 */
public class ZhuoYiFlowDeal implements BaseDeal
{
    Logger log = Logger.getLogger(getClass());
    
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> deal(Map<String, Object> order)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = -1;
        
        try
        {
            // 手机号
            String phone = (String)order.get("phone");
            String action = "Charge";
            // 平台订单号
            String c_id = ToolDateTime.format(new Date(), "yyyyMMddHHmmssSSS") + phone + GenerateData.getIntData(9, 4);
            map.put("orderId", c_id);
            String size = new Double(order.get("value") + "").intValue() + "";// 流量包大小
            // 账号
            Map<String, Object> channel = (Map<String, Object>)order.get("channel");// 获取通道参数
            String linkUrl = (String)channel.get("link_url");// 充值地址
            String defaultParameter = (String)channel.get("default_parameter");// 默认参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String account  = paramMap.get("account");// 账户名
            String range  = paramMap.get("range");//充值范围。0 :全国; 1 :省内
            String apikey = paramMap.get("apikey");
            long timeStamp = new Date().getTime()/1000;
            
    		String param = apikey+"account="+account+"&action="+action+"&phone="+phone+"&range="+range+"&size="+size+"&timeStamp="+timeStamp+apikey;
            String sign = MD5.ToMD5(param);
//    		JsonObject data = new JsonObject();
//    		data.addProperty("account", account);
//    		data.addProperty("action", action);
//    		data.addProperty("phone", phone);
//    		data.addProperty("range", range);
//    		data.addProperty("size", size);
//    		data.addProperty("timeStamp",timeStamp);// 设置签名
//    		data.addProperty("sign",sign);// 设置签名
//    		data.addProperty("orderNo",c_id);// 设置签名
    		String params ="account="+account+"&action="+action+"&phone="+phone+"&range="+range+"&size="+size+"&timeStamp="+timeStamp+"&orderNo="+c_id+ "&sign=" + sign;
    		String contentType = "application/x-www-form-urlencoded";
    		
    		String postUrl = linkUrl;
    		String retMsg = ToolHttp.post(false, postUrl,params, contentType);
            // 判断是否成功
            if (retMsg != null)
            {
                JSONObject jObject = JSONObject.parseObject(retMsg);
                String resultCode = jObject.getString("respCode");
                String resultMsg = jObject.getString("respMsg");
                String orderID = jObject.getString("orderID");
                map.put("resultCode", resultCode + ":" + resultMsg);
                if ("0000".equals(resultCode))
                {
                    map.put("providerOrderId", orderID);
                    flag = 1;
                }else {
                	flag = 0;
                }
            }
        }
        catch (ConnectTimeoutException | SocketTimeoutException e)
        {// 请求、响应超时
            flag = -1;
        }
        catch (Exception e)
        {
            log.error("卓移流量出错" + e + MapUtils.toString(order));
        }
        map.put("status", flag);
        return map;
    }
    
}
